﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace FtpLibDotNet
{
    internal class FtpStream : Stream, IDisposable
    {
        public override bool CanRead { get { return this.mode == FtpStreamMode.Download; } }
        public override bool CanWrite { get { return this.mode == FtpStreamMode.Upload; } }
        public override bool CanSeek { get { return false; } }
        public override long Length { get { throw new NotImplementedException(); } }
        public override long Position { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }

        private Socket socket;
        private FtpStreamMode mode;

        internal FtpStream(Socket socket, FtpStreamMode mode)
        {
            this.socket = socket;
            this.mode = mode;
        }

        public override void Flush()
        {
            throw new NotImplementedException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this.mode == FtpStreamMode.Upload)
                throw new NotImplementedException();
            return socket.Receive(buffer, offset, count, SocketFlags.None);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (this.mode == FtpStreamMode.Download)
                throw new NotImplementedException();
            this.socket.Send(buffer, offset, count, SocketFlags.None);
        }

        void IDisposable.Dispose()
        {
            if (socket.Connected) socket.Close();
        }
    }
}